package com.tarena.lbs.basic.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.repository.AdminRepository;
import com.tarena.lbs.basic.web.repository.BusinessRepository;
import com.tarena.lbs.basic.web.utils.AuthenticationContextUtils;
import com.tarena.lbs.common.passport.enums.Roles;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.po.BusinessPO;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminService {
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private BusinessRepository businessRepository;
    public AdminVO detail() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","认证解析失败!"));
        Integer adminId = userPrinciple.getId();
        AdminPO po = adminRepository.getById(adminId);
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(po,adminVO);
        return adminVO;
    }

    public PageResult<AdminVO> pageList(AdminQuery query) {
        PageResult<AdminVO> voPage = new PageResult<>(query.getPageSize(), query.getPageNo());
        PageInfo<AdminPO> pageInfo = getPageAdmin(query);
        voPage.setTotal(pageInfo.getTotal());
        List<AdminPO> pos = pageInfo.getList();
        List<AdminVO> vos = null;
        if (pos != null && pos.size() > 0){
            vos = pos.stream().map(po->{
                AdminVO vo = new AdminVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }

    private PageInfo<AdminPO> getPageAdmin(AdminQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> pos = adminRepository.list();
        return new PageInfo<>(pos);
    }

    public void addAdmin(AdminParam param) throws BusinessException {
        //1.验证平台管理员权限
        checkRole(Roles.ADMIN);
        //2.验证商家是否存在
        checkBusinessExist(param.getBusinessId());
        //3.验证手机号码是否重复
        checkPhoneReuse(param.getAccountPhone());
        //4.封装po用于向数据库插入数据
        AdminPO po = param2po(param);
        //5.数据库新增数据
        adminRepository.save(po);
    }

    private AdminPO param2po(AdminParam param) {
        AdminPO po = new AdminPO();
        BeanUtils.copyProperties(param,po);
        po.setAccountStatus(0);
        po.setCreateTime(new Date());
        po.setBusinessId(param.getAccountType()==0?0: param.getBusinessId());
        return po;
    }

    private void checkPhoneReuse(String accountPhone) throws BusinessException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account_phone",accountPhone);
        long count = adminRepository.count(queryWrapper);
        Asserts.isTrue(count > 0,new BusinessException("-2","号码重复"));
    }

    private void checkBusinessExist(Integer businessId) throws BusinessException {
        BusinessPO businessPO = businessRepository.getById(businessId);
        Asserts.isTrue(businessId==null,new BusinessException("-2","商家不存在"));
    }

    private void checkRole(Roles role) throws BusinessException {
        //1.获取上游传入的登录用户信息
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple == null,new BusinessException("-2","用户解析失败！"));
        Roles userRole = userPrinciple.getRole();
        Asserts.isTrue(role != userRole,new BusinessException("-2","只有平台管理员才有增加商家权限！"));
    }


}
